﻿using System; 
using System.Web.Mvc;
using System.Web.Routing;

using StructureMap;

namespace CreateYourWorld.Common
{
    public class StructureMapControllerFactory : DefaultControllerFactory
    {        
        protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
        {
            try
            {
                if (controllerType == null)
                {
                    return base.GetControllerInstance(requestContext, controllerType);
                }

                var controller = ObjectFactory.GetInstance(controllerType) as IController;

                if (controller == null)
                {
                    return base.GetControllerInstance(requestContext, controllerType);
                }
                else
                {
                    return controller;
                }
            }
            catch (StructureMapException)
            {
                throw;
            }
        }
    }
}